package Rangel.common.world.helper;

import Rangel.common.RangelConstants;
import Rangel.common.world.entity.Entrance;
import Rangel.common.world.entity.RangelBuilding;
import Rangel.common.world.entity.RangelRoad;
import adf.agent.info.AgentInfo;
import adf.agent.info.ScenarioInfo;
import adf.agent.info.WorldInfo;
import rescuecore2.standard.entities.Building;
import rescuecore2.standard.entities.Refuge;
import rescuecore2.standard.entities.Road;

import java.util.ArrayList;
import java.util.List;

public class BuildingHelper implements IHelper {//建筑

    private RangelWorldHelper worldHelper;
    protected ScenarioInfo scenarioInfo;
    protected AgentInfo agentInfo;
    protected WorldInfo worldInfo;


    public BuildingHelper(RangelWorldHelper worldHelper, ScenarioInfo scenarioInfo, AgentInfo agentInfo, WorldInfo worldInfo) {
        this.worldHelper = worldHelper;
        this.scenarioInfo = scenarioInfo;
        this.agentInfo = agentInfo;
        this.worldInfo = worldInfo;
    }

    @Override
    public void init() {

    }

    @Override
    public void update() {


        for (RangelBuilding RangelBuilding : worldHelper.getRangelBuildings()) {
            if (worldHelper.getBuildingsSeen().contains(RangelBuilding.getSelfBuilding())) {
                boolean reachable = false;
                if (RangelBuilding.isOneEntranceOpen(worldHelper)) {//是一个开放的入口
                    reachable = true;
                }
                RangelRoad RangelRoad;
                if (reachable) {
                    boolean tempReachable = false;//可以进入的温度
                    for (Road road : BuildingHelper.getEntranceRoads(worldHelper, RangelBuilding.getSelfBuilding())) {
                        RangelRoad = worldHelper.getRangelRoad(road.getID());
                        if (RangelRoad.isReachable()) {//可以到达
                            tempReachable = true;
                            break;
                        }
                    }
                    if (!tempReachable) {
                        reachable = false;
                    }
                }
                RangelBuilding.setReachable(reachable);
            } else {
                if (RangelBuilding.getSelfBuilding() instanceof Refuge) {
                    RangelBuilding.resetOldReachable(RangelConstants.REFUGE_PASSABLY_RESET_TIME);
                } else {
                    RangelBuilding.resetOldReachable(RangelConstants.BUILDING_PASSABLY_RESET_TIME);
                }
            }
            RangelBuilding.getCivilianPossibly().clear();
            if (isBuildingBurnt(RangelBuilding.getSelfBuilding())) {
                worldHelper.setBuildingVisited(RangelBuilding.getID(), false);
            }
        }
    }

    private boolean isBuildingBurnt(Building building) {//是否建筑物是否正在着火
        if (building == null || !building.isFierynessDefined()) {
            return false;
        }
        int fieriness = building.getFieryness();

        return fieriness != 0 && fieriness != 4 && fieriness != 5;
    }

    public static List<Road> getEntranceRoads(RangelWorldHelper world, Building building) {//进入的入口
        ArrayList<Road> entranceRoads = new ArrayList<>();
        RangelBuilding RangelBuilding = world.getRangelBuilding(building.getID());
        for(Entrance entrance : RangelBuilding.getEntrances()){
            entranceRoads.add(entrance.getNeighbour());
        }
        return entranceRoads;
    }
}
